/**
 * Classe permettant l'affichage des statistiques de l'application
 * @authors:Fabrice SARRA,  Bertrand GUILLAUMOND
 * @version 1.0
 */

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.text.*;

public class StatisticsFrame extends JFrame implements ActionListener
{

  private JPanel topPanel;
  private JPanel consoPanel;
  private JPanel timePanel;
  private JPanel timePanel2;
  private JPanel lastPanel;
  private JLabel FirstTitle;
  
		
  JLabel conso0;
  JLabel conso1;
  JLabel conso2;
  JLabel conso3;
  JLabel conso4;
  JLabel conso5;
  JLabel mconso;

  JLabel travel0;
  JLabel travel1;
  JLabel travel2;
  JLabel travel3;
  JLabel travel4;
  JLabel travel5;
  JLabel mtravel;

  JLabel wait0;
  JLabel wait1;
  JLabel wait2;
  JLabel wait3;
  JLabel wait4;
  JLabel wait5;
  JLabel mwait;

  JLabel customers;
  JLabel executionTime;
  
  private JButton reinit;
  private JButton refresh;
  private Statistics[] s = new Statistics[6];

    public StatisticsFrame(Statistics[] stats)
    {
		s = stats;
     
        getContentPane().setLayout(new GridLayout(5,1,20,20));
        
        topPanel = new JPanel();
        consoPanel = new JPanel();
        timePanel = new JPanel();
        timePanel2 = new JPanel();
	lastPanel = new JPanel();

		FirstTitle = new JLabel("<html><h1>Statistiques</h1></html>", JLabel.CENTER);
		reinit = new JButton("Réinitialiser");
		refresh = new JButton("Actualiser");

		topPanel.setLayout(new GridLayout(3,3,20,4));
		consoPanel.setLayout(new GridLayout(8,3,20,4));
		timePanel.setLayout(new GridLayout(8,3,20,4));
		timePanel2.setLayout(new GridLayout(8,3,20,4));
		lastPanel.setLayout(new GridLayout(3,2,20,4));
		   
		getContentPane().add(topPanel);
		getContentPane().add(consoPanel); 
		getContentPane().add(timePanel);
		getContentPane().add(timePanel2);
		getContentPane().add(lastPanel);   
			   
		topPanel.add(new JLabel(""));   
		topPanel.add(FirstTitle);
		topPanel.add(new JLabel(""));
		topPanel.add(new JLabel(""));
		topPanel.add(new JLabel(""));
		topPanel.add(new JLabel(""));
		topPanel.add(reinit);
		topPanel.add(new JLabel(""));
		topPanel.add(refresh);

			        
		    
		conso0 = new JLabel(s[0].getConso()+" kwH", JLabel.CENTER);
		conso1 = new JLabel(s[1].getConso()+" kwH", JLabel.CENTER);
		conso2 = new JLabel(s[2].getConso()+" kwH", JLabel.CENTER);
		conso3 = new JLabel(s[3].getConso()+" kwH", JLabel.CENTER);
		conso4 = new JLabel(s[4].getConso()+" kwH", JLabel.CENTER);
		conso5 = new JLabel(s[5].getConso()+" kwH", JLabel.CENTER);
		
		int cmoy = 0;
		for(int i = 0; i < 5 ; i++)
		{
			cmoy += s[i].getConso();
		}
		cmoy = cmoy /6;
		mconso = new JLabel("moyenne : "+cmoy +" kwH", JLabel.CENTER);
	
		JLabel titleConso = new JLabel("<html><h3><u>Consommation électrique.</html></h3></u>");
		titleConso.setHorizontalAlignment(JLabel.CENTER);
		consoPanel.add(titleConso);     
        
        consoPanel.add(new JLabel(""));
        consoPanel.add(mconso);
        consoPanel.add(new JLabel("Ascenseur 1 : ",JLabel.CENTER));
        consoPanel.add(new JLabel("Ascenseur 3 : ",JLabel.CENTER));
        consoPanel.add(new JLabel("Ascenseur 5 : ",JLabel.CENTER));
			
        consoPanel.add(conso0);
        consoPanel.add(conso2);
        consoPanel.add(conso4);
        consoPanel.add(new JLabel(""));
        consoPanel.add(new JLabel(""));
        consoPanel.add(new JLabel(""));
        consoPanel.add(new JLabel("Ascenseur 2 : ",JLabel.CENTER));
        consoPanel.add(new JLabel("Ascenseur 4 : ",JLabel.CENTER));
        consoPanel.add(new JLabel("Ascenseur 6 : ",JLabel.CENTER));
        consoPanel.add(conso1);
        consoPanel.add(conso3);
        consoPanel.add(conso5);
        consoPanel.add(new JLabel(""));
        consoPanel.add(new JLabel(""));
        consoPanel.add(new JLabel(""));
        
        JLabel titleTime = new JLabel("<html><h3><u>Temps de trajet moyen</html></h3></u>");
		titleTime.setHorizontalAlignment(JLabel.CENTER);
		timePanel.add(titleTime);     

		travel0 = new JLabel(s[0].getAverageTravelTime()+" Secs.", JLabel.CENTER);
   		travel1 = new JLabel(s[1].getAverageTravelTime()+" Secs.", JLabel.CENTER);
    	travel2 = new JLabel(s[2].getAverageTravelTime()+" Secs.", JLabel.CENTER);
		travel3 = new JLabel(s[3].getAverageTravelTime()+" Secs.", JLabel.CENTER);
    	travel4 = new JLabel(s[4].getAverageTravelTime()+" Secs.", JLabel.CENTER);
    	travel5 = new JLabel(s[5].getAverageTravelTime()+" Secs.", JLabel.CENTER);

		float tmoy = 0;
		for(int i = 0; i < 5 ; i++)
		{
			tmoy += s[i].getAverageTravelTime();
		}
		tmoy = tmoy /6;

		mtravel = new JLabel("moyen : "+tmoy+" Secs.", JLabel.CENTER);
       
        timePanel.add(new JLabel(""));
        timePanel.add(mtravel);
        timePanel.add(new JLabel("Ascenseur 1 : ",JLabel.CENTER));
        timePanel.add(new JLabel("Ascenseur 3 : ",JLabel.CENTER));
        timePanel.add(new JLabel("Ascenseur 5 : ",JLabel.CENTER));
        timePanel.add(travel0);
        timePanel.add(travel2);
        timePanel.add(travel4);
        timePanel.add(new JLabel(""));
        timePanel.add(new JLabel(""));
        timePanel.add(new JLabel(""));
        timePanel.add(new JLabel("Ascenseur 2 : ",JLabel.CENTER));
        timePanel.add(new JLabel("Ascenseur 4 : ",JLabel.CENTER));
        timePanel.add(new JLabel("Ascenseur 6 : ",JLabel.CENTER));
        timePanel.add(travel1);
        timePanel.add(travel3);
        timePanel.add(travel5);
        timePanel.add(new JLabel(""));
        timePanel.add(new JLabel(""));
        timePanel.add(new JLabel(""));

		JLabel titleTime2 = new JLabel("<html><h3><u>Temps d'attente moyen</html></h3></u>");
		titleTime2.setHorizontalAlignment(JLabel.CENTER);
		timePanel2.add(titleTime2);  

		wait0 = new JLabel(s[0].getAverageWaitTime()+" Secs.", JLabel.CENTER);
   		wait1 = new JLabel(s[1].getAverageWaitTime()+" Secs.", JLabel.CENTER);
    	wait2 = new JLabel(s[2].getAverageWaitTime()+" Secs.", JLabel.CENTER);
		wait3 = new JLabel(s[3].getAverageWaitTime()+" Secs.", JLabel.CENTER);
    	wait4 = new JLabel(s[4].getAverageWaitTime()+" Secs.", JLabel.CENTER);
    	wait5 = new JLabel(s[5].getAverageWaitTime()+" Secs.", JLabel.CENTER);	
	  
		float wmoy = 0;
		for(int i = 0; i < 5 ; i++)
		{
			wmoy += s[i].getAverageWaitTime();
		}
		wmoy = wmoy /6;

		mwait = new JLabel("moyen : "+tmoy+" Secs.", JLabel.CENTER);
    
        timePanel2.add(new JLabel());
        timePanel2.add(mwait);
        timePanel2.add(new JLabel("Ascenseur 1 : ",JLabel.CENTER));
        timePanel2.add(new JLabel("Ascenseur 3 : ",JLabel.CENTER));
        timePanel2.add(new JLabel("Ascenseur 5 : ",JLabel.CENTER));
        timePanel2.add(wait0);
        timePanel2.add(wait2);
		timePanel2.add(wait4);
        timePanel2.add(new JLabel(""));
        timePanel2.add(new JLabel(""));
        timePanel2.add(new JLabel(""));
        timePanel2.add(new JLabel("Ascenseur 2 : ",JLabel.CENTER));
        timePanel2.add(new JLabel("Ascenseur 4 : ",JLabel.CENTER));
        timePanel2.add(new JLabel("Ascenseur 6 : ",JLabel.CENTER));
        timePanel2.add(wait1);
        timePanel2.add(wait3);
		timePanel2.add(wait5);
        timePanel2.add(new JLabel(""));
        timePanel2.add(new JLabel(""));
        timePanel2.add(new JLabel(""));
	
        lastPanel.add(new JLabel("Nombre total de passagers : ",JLabel.CENTER));
        lastPanel.add(new JLabel("Durée de la simulation : ",JLabel.CENTER));
	
		int count = 0;		
		for(int i = 0; i < 6; i++)
			count += s[i].countCustomers();
				
		customers = new JLabel(count+" ",JLabel.CENTER);
		executionTime = new JLabel(Statistics.timeToWords(s[0].getExecutionTime()),JLabel.CENTER);
    	lastPanel.add(customers);
        lastPanel.add(executionTime);
      	lastPanel.add(new JLabel(""));
        lastPanel.add(new JLabel(""));

        addEvents();
        
        setTitle("Statistiques");
        setSize(700,850);
        setVisible(true);

    }

	public void addEvents()
	{
		refresh.addActionListener(this);
		reinit.addActionListener(this);
	}

	public void actionPerformed(ActionEvent ev)
	{
		if(ev.getSource() == refresh)
		{
			conso0.setText(s[0].getConso()+" kwH");
		    conso1.setText(s[1].getConso()+" kwH");
		    conso2.setText(s[2].getConso()+" kwH");
			conso3.setText(s[3].getConso()+" kwH");
		    conso4.setText(s[4].getConso()+" kwH");
		    conso5.setText(s[5].getConso()+" kwH");

			travel0.setText(s[0].getAverageTravelTime()+" Secs.");
	   		travel1.setText(s[1].getAverageTravelTime()+" Secs.");
			travel2.setText(s[2].getAverageTravelTime()+" Secs.");
			travel3.setText(s[3].getAverageTravelTime()+" Secs.");
			travel4.setText(s[4].getAverageTravelTime()+" Secs.");
			travel5.setText(s[5].getAverageTravelTime()+" Secs.");

			wait0.setText(s[0].getAverageWaitTime()+" Secs.");
	   		wait1.setText(s[1].getAverageWaitTime()+" Secs.");
			wait2.setText(s[2].getAverageWaitTime()+" Secs.");
			wait3.setText(s[3].getAverageWaitTime()+" Secs.");
			wait4.setText(s[4].getAverageWaitTime()+" Secs.");
			wait5.setText(s[5].getAverageWaitTime()+" Secs.");
		
			int count = 0;		
			for(int i = 0; i < 6; i++)
				count += s[i].countCustomers();
				
			customers.setText(count+" ");
			executionTime.setText(Statistics.timeToWords(s[0].getExecutionTime()));

			int cmoy = 0;
			for(int i = 0; i < 5 ; i++)
			{
				cmoy += s[i].getConso();
			}
			cmoy = cmoy /6;
			mconso.setText("moyen : "+cmoy +" kwH");

			float tmoy = 0;
			count = 0;
			for(int i = 0; i < 5 ; i++)
			{
				
				tmoy += s[i].getAverageTravelTime();
				if(s[i].getAverageTravelTime() > 0)
				count++;
			}
			tmoy = tmoy /count;
			count = 0;

			mtravel.setText("moyen : "+Math.floor(tmoy)+" Secs.");

			float wmoy = 0;
			for(int i = 0; i < 5 ; i++)
			{
				wmoy += s[i].getAverageWaitTime();
				if(s[i].getAverageWaitTime() > 0)
				count++;
			}
			wmoy = wmoy /count;

			mwait.setText("moyen : " + Math.floor(wmoy)+" Secs.");
			
		}

		if(ev.getSource() == reinit)
		{
			
			try{
				DateFormat format = new SimpleDateFormat("hh-mm-dd-MM");
				
				FileWriter fw = new FileWriter("./stat-"+format.format(new Date())+".txt");
				BufferedWriter output = new BufferedWriter(fw); 
	
				output.write("# Consommation énergétique\n");
				output.write("\n\t");
				output.write("Moyenne : "+mconso.getText());
				output.write("\n\t");
				output.write("Ascenseur 1 : "+s[0].getConso() +" kWh");
				output.write("\n\t");
				output.write("Ascenseur 2 : "+s[1].getConso() +" kWh");
				output.write("\n\t");
				output.write("Ascenseur 3 : "+s[2].getConso() +" kWh");
				output.write("\n\t");
				output.write("Ascenseur 4 : "+s[3].getConso() +" kWh");
				output.write("\n\t");
				output.write("Ascenseur 5 : "+s[4].getConso() +" kWh");
				output.write("\n\t");
				output.write("Ascenseur 6 : "+s[5].getConso() +" kWh");
				output.write("\n");
				output.write("\n");
				output.write("# Temps d'attente moyen\n");
				output.write("\n\t");
				output.write("Gobal : "+mwait.getText());
				output.write("\n\t");
				output.write("Ascenseur 1 : "+s[0].getAverageWaitTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 2 : "+s[1].getAverageWaitTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 3 : "+s[2].getAverageWaitTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 4 : "+s[3].getAverageWaitTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 5 : "+s[4].getAverageWaitTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 6 : "+s[5].getAverageWaitTime() +" secondes");
				output.write("\n");
				output.write("\n");
				output.write("# Temps de trajet moyen\n");
				output.write("\n\t");
				output.write("Global : "+mtravel.getText());
				output.write("\n\t");
				output.write("Ascenseur 1 : "+s[0].getAverageTravelTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 2 : "+s[1].getAverageTravelTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 3 : "+s[2].getAverageTravelTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 4 : "+s[3].getAverageTravelTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 5 : "+s[4].getAverageTravelTime() +" secondes");
				output.write("\n\t");
				output.write("Ascenseur 6 : "+s[5].getAverageTravelTime() +" secondes");
				output.write("\n");
				output.write("\n");
				output.write("# Simulation\n");
				output.write("Durée de simulation : "+Statistics.timeToWords(s[0].getExecutionTime()));
				output.write("\n");
				output.write("Nombre d'appels : " + customers.getText());
				
				output.write("\n");
				output.write("\n");

				output.flush(); 
		  		output.close();
			}
			catch(IOException e)
			{
			
			}
		
			s[0].erase();
		    s[1].erase();
			s[2].erase();
			s[3].erase();
			s[4].erase();
			s[5].erase();

			conso0.setText(s[0].getConso()+" kwH");
		    conso1.setText(s[1].getConso()+" kwH");
		    conso2.setText(s[2].getConso()+" kwH");
			conso3.setText(s[3].getConso()+" kwH");
		    conso4.setText(s[4].getConso()+" kwH");
		    conso5.setText(s[5].getConso()+" kwH");

			travel0.setText(s[0].getAverageTravelTime()+" Secs.");
	   		travel1.setText(s[1].getAverageTravelTime()+" Secs.");
			travel2.setText(s[2].getAverageTravelTime()+" Secs.");
			travel3.setText(s[3].getAverageTravelTime()+" Secs.");
			travel4.setText(s[4].getAverageTravelTime()+" Secs.");
			travel5.setText(s[5].getAverageTravelTime()+" Secs.");

			wait0.setText(s[0].getAverageWaitTime()+" Secs.");
	   		wait1.setText(s[1].getAverageWaitTime()+" Secs.");
			wait2.setText(s[2].getAverageWaitTime()+" Secs.");
			wait3.setText(s[3].getAverageWaitTime()+" Secs.");
			wait4.setText(s[4].getAverageWaitTime()+" Secs.");
			wait5.setText(s[5].getAverageWaitTime()+" Secs.");

			mconso.setText("moyen : 0 kwH");
			mtravel.setText("moyen : 0 Sec.");
			mwait.setText("moyen : 0 Sec.");

			int count = 0;		
			for(int i = 0; i < 6; i++)
				count += s[i].countCustomers();
				
			customers.setText(count+" ");

			executionTime.setText(Statistics.timeToWords(s[0].getExecutionTime()));
		}
		//setVisible(false);
	}

	
    
    
}
